package com.jt.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import redis.clients.jedis.*;
import redis.clients.jedis.util.ShardInfo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Configuration  //标识一个配置类,一般用来整合第三方
@PropertySource("classpath:/properties/redis.properties")
public class RedisConfig {

    @Value("${redis.nodes}")
    private String nodes;

    @Bean
    public JedisCluster jedisCluster(){
        HashSet<HostAndPort> nodeSet = new HashSet<>();
        String[] nodeArray = nodes.split(",");
        for (String node:nodeArray){
            String host = node.split(":")[0];
            int port = Integer.parseInt(node.split(":")[1]);
            nodeSet.add(new HostAndPort(host,port));
        }
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(1000);
        poolConfig.setMinIdle(10);
        poolConfig.setMaxIdle(100);
        return new JedisCluster(nodeSet,poolConfig);
    }







//    @Bean
//    public ShardedJedis shardedJedis(){
//        List<JedisShardInfo> shards=new ArrayList<>();
//        String[] nodeArray = nodes.split(",");
//        for (String node:nodeArray){
//            String host = node.split(":")[0];
//            int port = Integer.parseInt(node.split(":")[1]);
//            JedisShardInfo info = new JedisShardInfo(host, port);
//            shards.add(info);
//        }
//        return new ShardedJedis(shards);
//    }






//    @Value("${redis.host}")
//    private String host;
//    @Value("${redis.port}")
//    private Integer port;
//
//    @Bean
//    public Jedis jedis(){
//        return new Jedis(host,port);
//    }
}






















